package xjtu.se.articlecommunity.buser.articlemanage;

import org.springframework.web.bind.annotation.*;
import xjtu.se.articlecommunity.buser.articlemanage.dto.articleDto;
import xjtu.se.articlecommunity.buser.articlemanage.dto.at_tabDto;
import xjtu.se.articlecommunity.common.Result;
import xjtu.se.articlecommunity.common.SSE.MessageType;
import xjtu.se.articlecommunity.common.SSE.service.MessService;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/buser/articlemanage")
public class ArticleManageAPI {
    @Resource
    private ArticleManageService articleManageService;
    @Resource
    private MessService messService;

    /**
     * 依据条件查找文章列表
     * @param dto 查找的条件
     * @return 查找到的文章列表分页
     */
    @GetMapping("/list")
    public Result getArticleList(articleDto dto){
        Map<String, Object> artPage = articleManageService.getArticleList(dto);
        return Result.success(artPage);
    }

    /**
     * 设置文章的标签
     * @param dto 要设置的标签对象
     * @return 设置结果
     */
    @PostMapping("/setTab")
    public Result setArticleTab(@RequestBody articleDto dto){
        if(!articleManageService.isValidArticle(dto.getA_id())) {
            return Result.fail(Result.ERR_CODE_BUSINESS, "无效的文章id");
        }

        if (!articleManageService.isValidTabs(dto.getAt_list())){
            return Result.fail(Result.ERR_CODE_BUSINESS, "无效的标签");
        }

        articleManageService.setArticleTabs(dto.getA_id(), dto.getAt_list());
        return Result.success();
    }

    /**
     * 查询文章的标签
     * @param a_id 文章的id
     * @return 查询到的文章标签列表
     */
    @GetMapping("/queryTabs")
    public Result queryArticleTabs(String a_id){
        if(!articleManageService.isValidArticle(a_id)){
            return Result.fail(Result.ERR_CODE_BUSINESS, "无效的文章id");
        }

        List<at_tabDto> at_ids = articleManageService.queryTabList(a_id);
        return Result.success(at_ids);
    }

    @GetMapping("/queryAllTabs")
    public Result queryAllTabs(){
        List<at_tabDto> at_ids = articleManageService.queryALLTabs();
        return Result.success(at_ids);
    }

    /**
     * 删除文章标签
     * @param at_id 要删除的文章标签id
     * @return 删除的结果
     */
    @PostMapping("/deleteTab")
    public Result deleteTab(String at_id){
        List<String> tabData = Collections.singletonList(at_id);
        if(!articleManageService.isValidTabs(tabData)){
            return Result.fail(Result.ERR_CODE_BUSINESS, "无效的标签");
        }
        articleManageService.deleteTabByID(at_id);
        return Result.success();
    }

    /**
     * 生成新文章标签
     * @param at_name 要生成的文章标签名称
     * @return 生成结果
     */
    @PostMapping("/generateTab")
    public Result generateTab(String at_name){
        if (at_name==null || at_name.equals("")){
            return Result.fail(Result.ERR_CODE_BUSINESS, "未输入标签！");
        }
        if(articleManageService.findTabByName(at_name)>0){
            return Result.fail(Result.ERR_CODE_BUSINESS, "同名标签已存在");
        }
        return Result.success(articleManageService.generateTab(at_name));
    }

    /**
     * 设置文章审核状态
     * @param dto 要设置的审核状态
     * @return 设置的结果
     * @throws IOException 调用通知组件时可能的异常
     */
    @PostMapping("/setStatus")
    public Result setArticleStatus(@RequestBody articleDto dto) throws IOException {
        if (!articleManageService.isValidArticle(dto.getA_id())) {
            return Result.fail(Result.ERR_CODE_BUSINESS, "无效的文章id");
        }
        switch (dto.getA_status()){
            case 0:
                break;
            case 1:
                messService.sendMessage(MessageType.approve, null, dto.getA_id());
                break;
            case 2:
                messService.sendMessage(MessageType.disapprove, null, dto.getA_id());
                break;
            default:
                return Result.fail(Result.ERR_CODE_BUSINESS, "无效的文章状态码!");
        }
        articleManageService.setArticleStatus(dto);
        return Result.success();
    }
}
